/* @(#)FmmMediatorSqLite.java
 ** 
 ** Copyright (C) 2012 by Steven D. Stamps
 **
 **             Trademarks & Copyrights
 ** Flywheel Management Science(TM), Flywheel Management Model(TM),
** Flywheel Story Editor(TM) and FlywheelMS(TM) are exclusive trademarks
** of Steven D. Stamps and may only be used freely for the purpose of
** identifying the unforked version of this software.  Subsequent forks
** may not use these trademarks.  All other rights are reserved.
 **
 ** DecKanGL (Decorated Kanban Glyph Language) and TribKn (Tribal Knowledge)
** are also exclusive trademarks of Steven D. Stamps.  These may be used
** freely within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** gConGUI (game Controller Graphical User Interface) is an exclusive
** trademark of Steven D. Stamps.  This trademark may be used freely
** within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** Trademark information is available at
** <http://www.flywheelms.com/trademarks>
 **
 ** Flywheel Management Science(TM) is a copyrighted body of management
 ** metaphors, governance processes, and leadership techniques that is
 ** owned by Steven D. Stamps.  These copyrighted materials may be freely
 ** used, without alteration, by the community (users and developers)
 ** surrounding this GPL3-licensed software.  Additional copyright
 ** information is available at <http://www.flywheelms.org/copyrights>
 **
 **              GPL3 Software License
 ** This program is free software: you can use it, redistribute it and/or
 ** modify it under the terms of the GNU General Public License, version 3,
 ** as published by the Free Software Foundation. This program is distributed
 ** in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 ** even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 ** PURPOSE.  See the GNU General Public License for more details. You should
 ** have received a copy of the GNU General Public License, in a file named
 ** COPYING, along with this program.  If you cannot find your copy, see
 ** <http://www.gnu.org/licenses/gpl-3.0.html>.
 */

package com.flywheelms.library.fmm.database;

import java.util.ArrayList;
import java.util.Date;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.format.DateFormat;

import com.flywheelms.library.FmsLibraryApplication;
import com.flywheelms.library.fmm.FmmDatabaseMediator;
import com.flywheelms.library.fmm.database.column.FseDocumentColumnDefinition;
import com.flywheelms.library.fmm.database.sqlite.dao.CommunityMemberDaoSqLite;
import com.flywheelms.library.fmm.database.sqlite.dao.FiscalYearDaoSqLite;
import com.flywheelms.library.fmm.database.sqlite.dao.FlywheelTeamDaoSqLite;
import com.flywheelms.library.fmm.database.sqlite.dao.FseDocumentDaoSqLite;
import com.flywheelms.library.fmm.database.sqlite.dao.NodeFragGovernanceDaoSqLite;
import com.flywheelms.library.fmm.database.sqlite.dao.OrganizationDaoSqLite;
import com.flywheelms.library.fmm.database.sqlite.dao.ProjectAssetDaoSqLite;
import com.flywheelms.library.fmm.database.sqlite.dao.ProjectAssetLinkToWorkPackageDaoSqLite;
import com.flywheelms.library.fmm.database.sqlite.dao.StrategicCommitmentDaoSqLite;
import com.flywheelms.library.fmm.database.sqlite.dao.StrategicMilestoneDaoSqLite;
import com.flywheelms.library.fmm.database.sqlite.dao.WorkPackageDaoSqLite;
import com.flywheelms.library.fmm.helper.FmmDateHelper;
import com.flywheelms.library.fmm.helper.FmmOpenHelper;
import com.flywheelms.library.fmm.node.impl.commitment.StrategicCommitment;
import com.flywheelms.library.fmm.node.impl.enumerator.FmmNodeDictionary;
import com.flywheelms.library.fmm.node.impl.governable.FiscalYear;
import com.flywheelms.library.fmm.node.impl.governable.FlywheelTeam;
import com.flywheelms.library.fmm.node.impl.governable.FmsOrganization;
import com.flywheelms.library.fmm.node.impl.governable.ProjectAsset;
import com.flywheelms.library.fmm.node.impl.governable.StrategicMilestone;
import com.flywheelms.library.fmm.node.impl.governable.WorkPackage;
import com.flywheelms.library.fmm.node.impl.headline.CommunityMember;
import com.flywheelms.library.fmm.node.impl.link.ProjectAssetLinkToWorkPackage;
import com.flywheelms.library.fmm.node.impl.nodefrag.NodeFragGovernance;
import com.flywheelms.library.fse.model.FseDocument;

public class FmmDatabaseMediatorSqLite extends FmmDatabaseMediator {

	private SQLiteDatabase sqLiteDatabase;
	private ContentValues contentValues = new ContentValues();

	public FmmDatabaseMediatorSqLite(RepositoryConfiguration aDatabaseConfiguration) {
		super(aDatabaseConfiguration);
		initializeFiscalYearMap();
	}

	private SQLiteDatabase getSqLiteDatabase() {
		if(this.sqLiteDatabase == null) {
			this.sqLiteDatabase = FmmOpenHelper.getInstance().getWritableDatabase();
		}
		return this.sqLiteDatabase;
	}

	//////  Node - COMMUNITY MEMBER  ////////////////////////////////////////////////////////////////////////////////

	@SuppressWarnings("resource")
	@Override
	protected ArrayList<CommunityMember> dbRetrieveCommunityMemberList() {
		Cursor theCursor = getSqLiteDatabase().rawQuery("Select * from " + FmmNodeDictionary.COMMUNITY_MEMBER.getName(), null);
		return CommunityMemberDaoSqLite.getInstance().getObjectListFromCursor(theCursor);
	}

	@Override
	protected ArrayList<CommunityMember> dbRetrieveCommunityMemberList(
			FmsOrganization anOrganization) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected CommunityMember dbRetrieveCommunityMember(String aNodeId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void dbInsertCommunityMember(CommunityMember aCommunityMember) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dbDeleteCommunityMember(CommunityMember aCommunityMember) {
		// TODO Auto-generated method stub
		
	}

	//////  Node - FISCAL YEAR  ////////////////////////////////////////////////////////////////////////////////

	@SuppressWarnings("resource")
	@Override
	protected ArrayList<FiscalYear> dbRetrieveFiscalYearList() {
		Cursor theCursor = getSqLiteDatabase().rawQuery("Select * from " + FmmNodeDictionary.FISCAL_YEAR.getName(), null);
		return FiscalYearDaoSqLite.getInstance().getObjectListFromCursor(theCursor);
	}

	@Override
	protected ArrayList<FiscalYear> dbRetrieveFiscalYearList(
			FmsOrganization anOrganization) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected FiscalYear dbRetrieveFiscalYear(String aNodeId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void dbInsertFiscalYear(FiscalYear aFiscalYear) {
		// TODO Auto-generated method stub
	}

	@Override
	public void dbDeleteFiscalYear(FiscalYear aFiscalYear) {
		// TODO Auto-generated method stub

	}

	////  Node - FLYWHEEL TEAM  ////////////////////////////////////////////////////////////////////////////////

	@SuppressWarnings("resource")
	@Override
	protected ArrayList<FlywheelTeam> dbRetrieveFlywheelTeamList() {
		Cursor theCursor = getSqLiteDatabase().rawQuery("Select * from " + FmmNodeDictionary.FLYWHEEL_TEAM.getName(), null);
		return FlywheelTeamDaoSqLite.getInstance().getObjectListFromCursor(theCursor);
	}

	@Override
	protected ArrayList<FlywheelTeam> dbRetrieveFlywheelTeamList(
			FmsOrganization anOrganization) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected FlywheelTeam dbRetrieveFlywheelTeam(String aNodeId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void dbInsertFlywheelTeam(FlywheelTeam aFlywheelTeam) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dbDeleteFlywheelTeam(FlywheelTeam aFlywheelTeam) {
		// TODO Auto-generated method stub
		
	}

	//////  Node - NODE FRAG GOVERNANCE  ////////////////////////////////////////////////////////////////////////////////

	@SuppressWarnings("resource")
	@Override
	protected ArrayList<NodeFragGovernance> dbRetrieveNodeFragGovernanceList() {
		Cursor theCursor = getSqLiteDatabase().rawQuery("Select * from " + FmmNodeDictionary.NODE_FRAG_GOVERNANCE.getName(), null);
		return NodeFragGovernanceDaoSqLite.getInstance().getObjectListFromCursor(theCursor);
	}

	@Override
	protected NodeFragGovernance dbRetrieveNodeFragGovernance(String aNodeId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void dbInsertNodeFragGovernance(NodeFragGovernance aNodeFragGovernance) {
		ContentValues theContentValues = NodeFragGovernanceDaoSqLite.getInstance().buildContentValues(aNodeFragGovernance);
		getSqLiteDatabase().insert(NodeFragGovernance.class.getSimpleName(), null, theContentValues);
	}

	@Override
	public void dbDeleteNodeFragGovernance(NodeFragGovernance aNodeFragGovernance) {
		// TODO Auto-generated method stub

	}

	//////  Node - ORGANIZATION  ////////////////////////////////////////////////////////////////////////////////

	@SuppressWarnings("resource")
	@Override
	protected ArrayList<FmsOrganization> dbRetrieveOrganizationList() {
		Cursor theCursor = getSqLiteDatabase().rawQuery("Select * from " + FmmNodeDictionary.ORGANIZATION.getName(), null);
		return OrganizationDaoSqLite.getInstance().getObjectListFromCursor(theCursor);
	}

	@Override
	protected FmsOrganization dbRetrieveOrganization(String aNodeId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void dbInsertOrganization(FmsOrganization aOrganization) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dbDeleteOrganization(FmsOrganization aOrganization) {
		// TODO Auto-generated method stub

	}

	////  Node - PROJECT ASSET  ////////////////////////////////////////////////////////////////////////////////

	@SuppressWarnings("resource")
	@Override
	protected ArrayList<ProjectAsset> dbRetrieveProjectAssetList() {
		Cursor theCursor = getSqLiteDatabase().rawQuery("Select * from " + FmmNodeDictionary.PROJECT_ASSET.getName(), null);
		return ProjectAssetDaoSqLite.getInstance().getObjectListFromCursor(theCursor);
	}

	@Override
	protected ArrayList<ProjectAsset> dbRetrieveProjectAssetList(
			StrategicMilestone aStrategicMilestone) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected ProjectAsset dbRetrieveProjectAsset(String aNodeId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void dbInsertProjectAsset(ProjectAsset aProjectAsset) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dbDeleteProjectAsset(ProjectAsset aProjectAsset) {
		// TODO Auto-generated method stub

	}
	
	////  Repository Access - PROJECT ASSET LINK TO WORK PACKAGE

	@SuppressWarnings("resource")
	@Override
	protected ArrayList<ProjectAssetLinkToWorkPackage> dbRetrieveProjectAssetLinkToWorkPackageList() {
		Cursor theCursor = getSqLiteDatabase().rawQuery("Select * from " + FmmNodeDictionary.PROJECT_ASSET_LINK_TO_WORK_PACKAGE.getName(), null);
		return ProjectAssetLinkToWorkPackageDaoSqLite.getInstance().getObjectListFromCursor(theCursor);
	}

	@Override
	protected ArrayList<ProjectAssetLinkToWorkPackage> dbRetrieveProjectAssetLinkToWorkPackageList(
			StrategicMilestone aStrategicMilestone) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected ProjectAssetLinkToWorkPackage dbRetrieveProjectAssetLinkToWorkPackage(
			String aNodeId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void dbInsertProjectAssetLinkToWorkPackage(
			ProjectAssetLinkToWorkPackage aProjectAssetLinkToWorkPackage) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dbDeleteProjectAssetLinkToWorkPackage(
			ProjectAssetLinkToWorkPackage aProjectAssetLinkToWorkPackage) {
		// TODO Auto-generated method stub

	}

	//////Node - STRATEGIC COMMITMENT  ////////////////////////////////////////////////////////////////////////////////

	@SuppressWarnings("resource")
	@Override
	protected ArrayList<StrategicCommitment> dbRetrieveStrategicCommitmentList() {
		Cursor theCursor = getSqLiteDatabase().rawQuery("Select * from " + FmmNodeDictionary.STRATEGIC_COMMITMENT.getName(), null);
		return StrategicCommitmentDaoSqLite.getInstance().getObjectListFromCursor(theCursor);
	}

	@Override
	protected ArrayList<StrategicCommitment> dbRetrieveStrategicCommitmentList(
			FiscalYear aStrategicCommitment) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected StrategicCommitment dbRetrieveStrategicCommitment(String aNodeId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void dbInsertStrategicCommitment(
			StrategicCommitment aStrategicCommitment) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dbDeleteStrategicCommitment(
			StrategicCommitment aStrategicCommitment) {
		// TODO Auto-generated method stub

	}

	////  Node - STRATEGIC MILESTONE  ////////////////////////////////////////////////////////////////////////////////

	@SuppressWarnings("resource")
	@Override
	protected ArrayList<StrategicMilestone> dbRetrieveStrategicMilestoneList() {
		Cursor theCursor = getSqLiteDatabase().rawQuery("Select * from " + FmmNodeDictionary.STRATEGIC_MILESTONE.getName(), null);
		return StrategicMilestoneDaoSqLite.getInstance().getObjectListFromCursor(theCursor);
	}

	@Override
	protected ArrayList<StrategicMilestone> dbRetrieveStrategicMilestoneList(
			FiscalYear aFiscalYear) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected StrategicMilestone dbRetrieveStrategicMilestone(String aNodeId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void dbInsertStrategicMilestone(
			StrategicMilestone aStrategicMilestone) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dbDeleteStrategicMilestone(
			StrategicMilestone aStrategicMilestone) {
		// TODO Auto-generated method stub

	}

	////  Node - WORK PACKAGE  ////////////////////////////////////////////////////////////////////////////////

	@SuppressWarnings("resource")
	@Override
	protected ArrayList<WorkPackage> dbRetrieveWorkPackageList() {
		Cursor theCursor = getSqLiteDatabase().rawQuery("Select * from " + FmmNodeDictionary.WORK_PACKAGE.getName(), null);
		return WorkPackageDaoSqLite.getInstance().getObjectListFromCursor(theCursor);
	}

	@Override
	protected ArrayList<WorkPackage> dbRetrieveWorkPackageList(
			FiscalYear aFiscalYear) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected WorkPackage dbRetrieveWorkPackage(String aNodeId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void dbInsertWorkPackage(WorkPackage aWorkPackage) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dbDeleteWorkPackage(WorkPackage aWorkPackage) {
		// TODO Auto-generated method stub
		
	}

	////  Node - FSE DOCUMENT  ////////////////////////////////////////////////////////////////////////////////


	@SuppressWarnings("resource")
	@Override
	protected ArrayList<FseDocument> dbRetrieveFseDocumentList() {
		Cursor theCursor = getSqLiteDatabase().rawQuery("Select * from " + FmmNodeDictionary.FSE_DOCUMENT.getName(), null);
		return FseDocumentDaoSqLite.getInstance().getObjectListFromCursor(theCursor);
	}

	@Override
	protected FseDocument dbRetrieveFseDocument(String aDocumentId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected FseDocument dbRetrieveFseDocumentForParent(String aNodeIdString) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void dbInsertFseDocument(FseDocument anFseDocument) {
        this.contentValues.clear();
        Date theDbTimeStamp = new Date();
        anFseDocument.setDbTimestamp(theDbTimeStamp);
        this.contentValues.put(FseDocumentColumnDefinition.column_ID, anFseDocument.getNodeIdString());
        this.contentValues.put(FseDocumentColumnDefinition.column_PARENT_ID, anFseDocument.getParentNodeIdString());
        this.contentValues.put(FseDocumentColumnDefinition.column_TIMESTAMP, DateFormat.getDateFormat(FmsLibraryApplication.getContext()).format(theDbTimeStamp));
        this.contentValues.put(FseDocumentColumnDefinition.column_SERIALIZED_DOCUMENT, anFseDocument.getSerialized());
        	getSqLiteDatabase().insert(FmmNodeDictionary.FSE_DOCUMENT.getClassName(), null, this.contentValues);
	}
	
	@Override
	protected void dbInsertFseDocumentTransaction(FseDocument anFseDocument) {
		getSqLiteDatabase().beginTransaction();
		try {
			dbInsertFseDocument(anFseDocument);
			getSqLiteDatabase().setTransactionSuccessful();
		} finally {
			getSqLiteDatabase().endTransaction();
		}
	}

	public Date getDbTimestamp(String aClassName, String aNodeIdString) {
		Cursor theCursor = getSqLiteDatabase().rawQuery(
				"SELECT timestamp FROM " + aClassName +
				"WHERE _id = " + aNodeIdString,
				null );
		Date theDate = null;
		if(theCursor.getCount() > 0) {
			long theLong = theCursor.getLong(theCursor.getColumnIndex(FseDocumentColumnDefinition.column_TIMESTAMP));
			theDate = FmmDateHelper.getDateFromFormattedUtcLong(theLong);
		}
		theCursor.close();
		return theDate;
	}

	@Override
	public void dbDeleteFseDocument(FseDocument anFseDocument) {
		// TODO Auto-generated method stub
		
	}

}
